package me.kagami.myplugin.form;

import com.intellij.ide.*;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.impl.ProjectImpl;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.ui.popup.list.ListPopupImpl;
import com.intellij.util.Alarm;
import com.intellij.util.SingleAlarm;
import me.kagami.myplugin.extensions.CustomizeSettingsState;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;

public class CustomizeToolWindow {
    private JButton button1;
    private JPanel content;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JButton button10;
    private JButton button11;
    private JButton button12;

    public CustomizeToolWindow() {
        initQuickStart();
        initQuickProject();
    }

    private void initQuickProject() {
        CustomizeSettingsState instance = CustomizeSettingsState.getInstance();

        button7.addActionListener(e -> doOpen(instance.getQuickProject1()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName1())) {
            button7.setText(instance.getQuickProjectSimpleName1());
        } else {
            button7.setText(instance.getQuickProject1());
        }
        button8.addActionListener(e -> doOpen(instance.getQuickProject2()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName2())) {
            button8.setText(instance.getQuickProjectSimpleName2());
        } else {
            button8.setText(instance.getQuickProject2());
        }
        button9.addActionListener(e -> doOpen(instance.getQuickProject3()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName3())) {
            button9.setText(instance.getQuickProjectSimpleName3());
        } else {
            button9.setText(instance.getQuickProject3());
        }
        button10.addActionListener(e -> doOpen(instance.getQuickProject4()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName4())) {
            button10.setText(instance.getQuickProjectSimpleName4());
        } else {
            button10.setText(instance.getQuickProject4());
        }
        button11.addActionListener(e -> doOpen(instance.getQuickProject5()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName5())) {
            button11.setText(instance.getQuickProjectSimpleName5());
        } else {
            button11.setText(instance.getQuickProject5());
        }
        button12.addActionListener(e -> doOpen(instance.getQuickProject6()));
        if (StringUtils.isNotEmpty(instance.getQuickProjectSimpleName6())) {
            button12.setText(instance.getQuickProjectSimpleName6());
        } else {
            button12.setText(instance.getQuickProject6());
        }
    }


    private void doOpen(String quickProject) {
        ReopenProjectAction reopenProjectAction = new ReopenProjectAction(quickProject, null, quickProject);
        DataContext dataContext = DataManager.getInstance().getDataContext(button7);
        reopenProjectAction.actionPerformed(new AnActionEvent(null, dataContext, "ExtraPopupAction", new Presentation(), ActionManager.getInstance(), 0));
    }

    private void initQuickStart() {
        String quickStart1 = CustomizeSettingsState.getInstance().getQuickStart1();
        if (StringUtils.isNotEmpty(quickStart1)) {
            button1.addActionListener(e -> doExe(quickStart1));
            File f = new File(quickStart1);
            button1.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button1.setText(f.getName());
        }

        String quickStart2 = CustomizeSettingsState.getInstance().getQuickStart2();
        if (StringUtils.isNotEmpty(quickStart2)) {
            button2.addActionListener(e -> doExe(quickStart2));
            File f = new File(quickStart2);
            button2.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button2.setText(f.getName());
        }

        String quickStart3 = CustomizeSettingsState.getInstance().getQuickStart3();
        if (StringUtils.isNotEmpty(quickStart3)) {
            button3.addActionListener(e -> doExe(quickStart3));
            File f = new File(quickStart3);
            button3.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button3.setText(f.getName());
        }

        String quickStart4 = CustomizeSettingsState.getInstance().getQuickStart4();
        if (StringUtils.isNotEmpty(quickStart4)) {
            button4.addActionListener(e -> doExe(quickStart4));
            File f = new File(quickStart4);
            button4.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button4.setText(f.getName());
        }

        String quickStart5 = CustomizeSettingsState.getInstance().getQuickStart5();
        if (StringUtils.isNotEmpty(quickStart5)) {
            button5.addActionListener(e -> doExe(quickStart5));
            File f = new File(quickStart5);
            button5.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button5.setText(f.getName());
        }

        String quickStart6 = CustomizeSettingsState.getInstance().getQuickStart6();
        if (StringUtils.isNotEmpty(quickStart6)) {
            button6.addActionListener(e -> doExe(quickStart6));
            File f = new File(quickStart6);
            button6.setIcon(FileSystemView.getFileSystemView().getSystemIcon(f));
            button6.setText(f.getName());
        }
    }

    private void doExe(String programPath) {
        if (StringUtils.isNotBlank(programPath)) {
            try {
                Desktop.getDesktop().open(new File(programPath));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    public JPanel getContent() {
        return content;
    }

}
